package cn.xny.pet.service.impl;


import cn.xny.basic.service.BaiduAiAuditService;
import cn.xny.basic.util.AjaxResult;
import cn.xny.pet.domain.Pet;
import cn.xny.pet.domain.PetDetail;
import cn.xny.pet.domain.PetOnlineAuditLog;
import cn.xny.pet.mapper.PetDetailMapper;
import cn.xny.pet.mapper.PetMapper;
import cn.xny.pet.mapper.PetOnlineAuditLogMapper;
import cn.xny.pet.service.PetService;
import cn.xny.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 业务实现类：
 */
@Service
public class PetServiceImpl extends BaseServiceImpl<Pet> implements PetService {


    @Autowired
     private PetMapper petMapper;

    @Autowired
    private PetDetailMapper petDetailMapper;
    @Autowired
    private BaiduAiAuditService baiduAiAuditService;
    @Autowired
    private PetOnlineAuditLogMapper petOnlineAuditLogMapper;

    @Transactional
    @Override
    public void save(Pet pet) {
        super.save(pet);
        PetDetail petDetail = pet.getPetDetail();
        if(petDetail!=null){

            petDetail.setPetId(pet.getId());
            //添加宠物详情
            petDetailMapper.save(petDetail);
        }


    }
    @Transactional
    @Override
    public void del(Long id) {
        //删除宠物详情通过宠物id
        petDetailMapper.delByPetId(id);

        super.del(id);
    }
    @Transactional
    @Override
    public void update(Pet pet) {
        super.update(pet);
        PetDetail petDetail = pet.getPetDetail();
        if(petDetail!=null){
            petDetail.setPetId(pet.getId());
            //修改宠物详情通过宠物id
            petDetailMapper.updateBypetId(petDetail);

        }


    }
    @Transactional
    @Override
    public void patchDelete(List<Long> ids) {
        for (Long id : ids) {
            del(id);
        }
    }
    //上架

    @Transactional
    @Override
    public AjaxResult onsale(List<Long> ids) {
        for (Long id : ids) {
            //先获取每个对象
            Pet pet = petMapper.loadByAll(id);

            //审核名字与图片
            Boolean Bool1 = baiduAiAuditService.textAudit(pet.getName());
            Boolean Bool2 = baiduAiAuditService.imageAudit(pet.getResources());
            PetOnlineAuditLog log = new PetOnlineAuditLog();
            if(Bool1 && Bool2){
               petMapper.onSale(id,new Date());

                log.setState(1);
                log.setNote("审核成功");

                petOnlineAuditLogMapper.save(log);


            }else {

                log.setState(0);
                log.setNote("审核失败");

                petOnlineAuditLogMapper.save(log);
               return AjaxResult.me().setSuccess(false).setMsg("上架失败");

            }



        }

     return new AjaxResult();


    }
    @Transactional
    //下架
    @Override
    public void offsale(List<Long> ids) {

        petMapper.offSale(ids,new Date());

    }
}
